<?php
/**
 * ebiz front
 *
 * direct util
 *
 * @author allen
 *
 */

//require_once 'Init.php';
//require_once '../app/controllers/CategoryController.php';
require_once 'core/utils/HttpUtil.class.php';
require_once 'constants/ConfigConstants.php';

function direct(){
	$action = "";
	$param = array();
	$strurl = "";
	$primalURL = "";
	$urlparam = "";
	$urlnum = "";
//	$actionname = "";
	try {
	    $strurl = $_SERVER['REQUEST_URI'];   //for example:if url is http://localhost:8080/f, output '/f'
	    $primalURL = explode('?', $strurl);    //declare if have ? send parameters, eg:http://localhost:8080/qqq?a=111 result=> array(2) { [0]=>  string(4) "/qqq" [1]=>  string(5) "a=111" }
        $debug = HttpUtil::getGetString("debug");
	    if($debug == 'true'){
            $GLOBALS["debug"] = 1;
        }else{
        	$GLOBALS["debug"] = 0;
        }
        
        /*require_once '../app/services/AAServiceTest.php';
        $userNumber = AAServiceTest::getUserNumber();
        if(!isset($userNumber) || empty($userNumber)){
        	require_once '../app/component/Number.php';
        	$userNumber = Number::getCustomerNumber();
        	AAServiceTest::setUserNumber($userNumber);
        }*/
        
	    $urlparam = explode('/', $primalURL[0]);     //set the url param if not root document, split with '/'
	    $urlnum = count($urlparam);    //distinguish if==2---Marketing Page
	    $actionname = 'index';     //set default action name
	    if(!trim(strtolower($urlparam[1]))){   //this is root document --> '/'
            home($actionname);
        } elseif (trim(strtolower($urlparam[1])) && isset($urlparam[2])){    //eg: at lease /xxx/xxx model or /xxx/xxx/xxx/xxx/...
        	//there may be a number of situations
        	control($actionname, $urlparam);
	    }elseif ($urlnum == 2 && strlen((trim($urlparam[1]))) > 1) {        //Marketing Page    /[page name]
            marketing($actionname, $urlparam);
	    }else{     //if it not is  root document and lease than /xxx/xxx(have one '/') model and not is Marketing Page
            error($actionname);
	    }
	}catch (Exception $e){
        error($actionname);
    }

}

    function control($actionname, $urlparam){
        if(trim(strtolower($urlparam[1])) == 'f' && !isset($urlparam[3])){    //if begin with 'f'
            //$action = 'feature';
            feature($actionname, $urlparam);
        }elseif (trim(strtolower($urlparam[1])) == 'u' && !isset($urlparam[3])){
            //$action = 'utility ';
            utility($actionname, $urlparam);
        }elseif (trim(strtolower($urlparam[1])) == 'userinfo' && !isset($urlparam[4])){
            //$action = 'userinfo ';
            userinfo($actionname, $urlparam);
        }elseif (trim(strtolower($urlparam[1])) == 'trade' && !isset($urlparam[4])){
            //$action = 'trade ';
            trade($actionname, $urlparam);
        //deleted by xiaoyuemian, 2010-1-6 12:45
        /*}elseif (trim(strtolower($urlparam[1])) == 'a'){    //if begin with 'a'
            //$action = 'ajax';
            $actionname = trim(strtolower($urlparam[2]));//0718 add
            ajax($actionname, $urlparam);
        */
        /* url第二级参数为'at'时，定向至ajaxTest函数，
         * 调用测试版二级页面
         * Added by xiaoyuemian, 2009-11-11 23:25
         * -- the begin
         */
        }elseif (trim(strtolower($urlparam[1])) == 'a'){    //if begin with 'at'
            //$action = 'ajax';
            $actionname = trim(strtolower($urlparam[2]));//0718 add
            ajaxTest($actionname, $urlparam);
        /* Added by xiaoyuemian -- the end */
        
        }elseif (trim(strtolower($urlparam[1])) == 's'){    //if begin with 's'
            //$action = 'search';
            search($actionname, $urlparam);
        //deleted by xiaoyuemian, 2010-1-8 18:32
        /* url第二级参数为'st'时，定向至searchTest函数，
         * 调用测试版二级页面
         * Added by xiaoyuemian, 2009-11-07 22:50
         * -- the begin
         */
        /*}elseif (trim(strtolower($urlparam[1])) == 'st'){    //if begin with 'st'
            //$action = 'search';
            searchTest($actionname, $urlparam);*/
        /* Added by xiaoyuemian -- the end */
        }elseif (trim(strtolower($urlparam[1])) == 'r'){  //if begin with 'r'
            //$action = 'rest';
            rest($actionname, $urlparam);
        }elseif (trim(strtolower($urlparam[1])) == 'o' && !isset($urlparam[3])){  //if begin with 'o'
            //$action = 'post';
            $actionname = trim(strtolower($urlparam[2]));
            post($actionname, $urlparam);
        }elseif (trim(strtolower($urlparam[1])) == 'p' && trim($urlparam[2]) && !isset($urlparam[6])){    //if begin with 'p', model is: /p/{url_title} or /p/{url_title}/{item_id} or /p/{url_title}/rec/{0 or 1} or /p/{url_title}/{item_id}/rec/{0 or 1}
             //$action = 'product';
             product($actionname, $urlparam);
        //added by xiaoyuemian, 2010-02-08 22:43
       /* }elseif (trim(strtolower($urlparam[1])) == 'pt' && trim($urlparam[2]) && !isset($urlparam[4])){    //if begin with 'p', model is: /p/{url_title} or /p/{url_title}/{item_id}
             //$action = 'product';
             productTest($actionname, $urlparam);*/
        
        //deleted by xiaoyuemian, 2010-1-6 12:45
        /*}elseif (trim(strtolower($urlparam[1])) == 'c' && trim(strtolower($urlparam[2]))){    //declare if Category Page is /c/[category id]/[page number]
            //$action = 'category';
            $actionname = trim(strtolower($urlparam[2]));
            category($actionname, $urlparam);
        */
        /* url第二级参数为'ct'时，定向至categoryTest函数，
         * 调用测试版二级页面
         * Added by xiaoyuemian, 2009-11-07 22:34
         * -- the begin
         */
        }elseif (trim(strtolower($urlparam[1])) == 'c' && trim(strtolower($urlparam[2]))){    //declare if Category Page is /ct/[category id]/[page number]
            //$action = 'category';
            $actionname = trim(strtolower($urlparam[2]));
            categoryTest($actionname, $urlparam);
        /* Added by xiaoyuemian -- the end*/
        }elseif (strlen(trim(strtolower($urlparam[1]))) > 1 && trim(strtolower($urlparam[2])) && trim(strtolower($urlparam[3]))) {   //declare if is /[product SEO name]/p/[product id] or /[category SEO name]/c/[category id]/[page number]
            if(trim(strtolower($urlparam[2])) == 'p' && !isset($urlparam[4])){
                //$action = 'product';
                productAll($actionname, $urlparam);
            }
            //Added by xiaoyuemian, 2010-02-08 22:45
        	/*elseif(trim(strtolower($urlparam[2])) == 'pt' && !isset($urlparam[4])){
                //$action = 'product';
                productAllTest($actionname, $urlparam);
            }*/
            //Added bye xiaoyuemian -- the end
            else{         //if it not equal 'p' or 'c', goto error page
                //$action = 'error';
                error($actionname);
            }
        }else{  //if not 'f' 's' 'r' 'o' 'p' 'c...' and not is /[product SEO name]/p/[product id] or /[category SEO name]/c/[category id]/[page number] model
            //$action = 'error';
            error($actionname);
        }
    }

	function home($actionname){
		require_once '../app/controllers/HomeController.php';
	    call_user_func(array('HomeController', $actionname));
	}

	function marketing($actionname, $urlparam){
		$param = array();      //set the pass parameters
		require_once '../app/controllers/MarketingController.php';
        $param['key'] = $urlparam[1];  //set the Marketing key
        call_user_func(array('MarketingController', strtolower($actionname)), $param);
	}

    function feature($actionname, $urlparam){
    	$param = array();
        require_once '../app/controllers/FeatureController.php';
        $param['featurePage'] = $urlparam[2];
        call_user_func(array('FeatureController', 'index'), $param);
//        $actionname = $urlparam[2];
//        if(!empty($_GET)){
//        	call_user_func(array('FeatureController', $actionname), $_GET);
//        }else{
//        	call_user_func(array('FeatureController', $actionname));
//        }
    }

    function rest($actionname, $urlparam){
    	$param = array();      //set the pass parameters
    	require_once '../app/controllers/RestController.php';
        $param['requestname'] = $urlparam[2];
        for($i = 3; $i < count($urlparam); $i++){  //set the all parameters from the $urlparam to $param
            $param[$i-3] = $urlparam[$i];
        }
        if(!empty($_GET)){
            $param['get'] = $_GET;
            call_user_func(array('RestController', strtolower($actionname)), $param);
        }else{
            call_user_func(array('RestController', strtolower($actionname)), $param);
        }
    }

    function post($actionname, $urlparam){
        require_once '../app/controllers/PostController.php';
        call_user_func(array('PostController', strtolower($actionname)));
    }

    function search($actionname, $urlparam){
        require_once '../app/controllers/SearchController.php';
        call_user_func(array('SearchController', 'index'), $urlparam);
    }
    
    /* 调用测试版二级页面controller——SearchControllerTest
     * Added by xiaoyuemian, 2009-11-07 22:51 
     * -- the begin
     */
    function searchTest($actionname, $urlparam){
        require_once '../app/controllers/SearchControllerTest.php';
        call_user_func(array('SearchControllerTest', 'index'), $urlparam);
    }
    /* Added by xiaoyuemian -- the end*/

    function product($actionname, $urlparam){
    	$param = array();      //set the pass parameters
        require_once '../app/controllers/ProductController.php';
        $param['productUrlTitle'] = $urlparam[2];
        $recIndex = 3;
        if(isset($urlparam[3])){
        	if($urlparam[3] != 'reqid'){
        		$param['itemId'] = $urlparam[3];
        		$recIndex++;
        	}
        	else
        		$param['itemId'] = "";
        	if (isset($urlparam[$recIndex]) && 
        		($urlparam[$recIndex] == 'reqid') && 
        		isset($urlparam[$recIndex + 1]))
        		$param['reqId'] = $urlparam[$recIndex + 1];
        	else
        		$param['reqId'] = null;
        }
        else {
	        $param['itemId'] = "";
	        $param['reqId'] = null;
        }
//        $param['itemId'] = (isset($urlparam[3]) && ($urlparam[3] != 'rec')) ? $urlparam[3] : "";
        call_user_func(array('ProductController', strtolower($actionname)), $param);
    }
    
    function productTest($actionname, $urlparam){
    	$param = array();      //set the pass parameters
        require_once '../app/controllers/ProductControllerTest.php';
        $param['productUrlTitle'] = $urlparam[2];
        $param['itemId'] = (isset($urlparam[3]) && ($urlparam[3] != 'rec')) ? $urlparam[3] : "";
        call_user_func(array('ProductControllerTest', strtolower($actionname)), $param);
    }

    function productAll($actionname, $urlparam){
    	$param = array();      //set the pass parameters
        require_once '../app/controllers/ProductController.php';
        $param['productSEOname'] = $urlparam[1];   //set the SEO name
        $param['productUrlTitle'] = $urlparam[3];    //set the productid
        call_user_func(array('ProductController', strtolower($actionname)), $param);
    }
    
    function productAllTest($actionname, $urlparam){
    	$param = array();      //set the pass parameters
        require_once '../app/controllers/ProductControllerTest.php';
        $param['productSEOname'] = $urlparam[1];   //set the SEO name
        $param['productUrlTitle'] = $urlparam[3];    //set the productid
        call_user_func(array('ProductControllerTest', strtolower($actionname)), $param);
    }

    function category($actionname, $urlparam){
//    	$param = array();      //set the pass parameters
//        require_once '../app/controllers/CategoryController.php';
//        $param['categorySEOName'] = $urlparam[2];
//        $num = isset($urlparam[3]) ? $urlparam[3] : "";
//        if(is_numeric($num) || $num == ""){     //if pagenumber is not null or it is a number
//	        if($num == ""){    //declare if pagenumber do not have value
//	            $param['pagenumber'] = 1;    //set default value---1
//	        }else{
//	            $param['pagenumber'] = intval($urlparam[3]);     //get the value
//	        }
//	        call_user_func(array('CategoryController', 'index'), $param);
//        }else{     //if pagenumber is not a number or is not null
//            error($actionname);
//        }
        require_once '../app/controllers/CategoryController.php';
//        $actionname = $urlparam[2];
        $GLOBALS['config']['urlparam'] = $urlparam;
        array_shift($urlparam);
        array_shift($urlparam);
        array_shift($urlparam);
        call_user_func(array('CategoryController', $actionname), $urlparam);
    }
    
    /* 调用测试版二级页面controller——CategoryControllerTest
     * Added by xiaoyuemian, 2009-11-07 22:34 
     * -- the begin
     */
    function categoryTest($actionname, $urlparam){
//    	$param = array();      //set the pass parameters
//        require_once '../app/controllers/CategoryController.php';
//        $param['categorySEOName'] = $urlparam[2];
//        $num = isset($urlparam[3]) ? $urlparam[3] : "";
//        if(is_numeric($num) || $num == ""){     //if pagenumber is not null or it is a number
//	        if($num == ""){    //declare if pagenumber do not have value
//	            $param['pagenumber'] = 1;    //set default value---1
//	        }else{
//	            $param['pagenumber'] = intval($urlparam[3]);     //get the value
//	        }
//	        call_user_func(array('CategoryController', 'index'), $param);
//        }else{     //if pagenumber is not a number or is not null
//            error($actionname);
//        }
        require_once '../app/controllers/CategoryControllerTest.php';
//        $actionname = $urlparam[2];
        $GLOBALS['config']['urlparam'] = $urlparam;
        array_shift($urlparam);
        array_shift($urlparam);
        array_shift($urlparam);
        call_user_func(array('CategoryControllerTest', $actionname), $urlparam);
    }
    /* Added by xiaoyuemian -- the end */

    function error($actionname){
//    	require_once '../app/views/error/404.php';
//    	exit;
        HttpUtil::redirect404();
//        require_once '../app/controllers/ErrorController.php';
//        call_user_func(array('ErrorController', strtolower($actionname)));
    }

    function utility($actionname, $urlparam){
        require_once '../app/controllers/UtilityController.php';
        $actionname = $urlparam[2];
        call_user_func(array('UtilityController', strtolower($actionname)));
   }

    function userinfo($actionname, $urlparam){
        require_once '../app/controllers/UserInfoController.php';
        $actionname = $urlparam[2];
        $GLOBALS['config']['urlparam'] = $urlparam;
        call_user_func(array('UserInfoController', strtolower($actionname)));
   }

    function trade($actionname, $urlparam){
    	$param = array();      //set the pass parameters
        require_once '../app/controllers/TradeController.php';
        $actionname = $urlparam[2];
    	if ($actionname != "supplementlogs" && $actionname != "applypayment"
    	   && $actionname != "mytradelogs")
    	{
    		header("Location: http://".ConfigConstants::$domainPath."/");
    	}
        $GLOBALS['config']['urlparam'] = $urlparam;
        $param['value'] = isset($urlparam[3]) ? $urlparam[3] : "";
        call_user_func(array('TradeController', strtolower($actionname)), $param);
   }

    function ajax($actionname, $urlparam){
        $param = array();      //set the pass parameters
        require_once '../app/controllers/AjaxController.php';
//        $param['requestname'] = $urlparam[2];
//        for($i = 3; $i < count($urlparam); $i++){  //set the all parameters from the $urlparam to $param
//            $param[$i-3] = $urlparam[$i];
//        }
//        if(!empty($_GET)){
//        	$param['get'] = $_GET;
//            call_user_func(array('AjaxController', $actionname), $param);
//        }else{
//        	call_user_func(array('AjaxController', $actionname), $param);
//        }
        //0718 add //move post contriller function to there
        call_user_func(array('AjaxController', strtolower($actionname)),$urlparam);
    }

    /* 调用测试版二级页面controller——CategoryControllerTest
     * Added by xiaoyuemian, 2009-11-11 23:36 
     * -- the begin
     */
    function ajaxTest($actionname, $urlparam){
        $param = array();      //set the pass parameters
        require_once '../app/controllers/AjaxControllerTest.php';
//        $param['requestname'] = $urlparam[2];
//        for($i = 3; $i < count($urlparam); $i++){  //set the all parameters from the $urlparam to $param
//            $param[$i-3] = $urlparam[$i];
//        }
//        if(!empty($_GET)){
//        	$param['get'] = $_GET;
//            call_user_func(array('AjaxController', $actionname), $param);
//        }else{
//        	call_user_func(array('AjaxController', $actionname), $param);
//        }
        //0718 add //move post contriller function to there
        call_user_func(array('AjaxControllerTest', strtolower($actionname)),$urlparam);
    }
    /* Added by xiaoyuemian -- the end */
